#!/bin/bash

DOMAIN=#%domain%#
RSYNC_USER=#%rsync_user%#
RSYNC_MODULE=#%rsync_module%#
SERVER_DIR=#%server_dir%#
CLIENT_PATH=#%client_path%#

menu(){
    printf "########## 请选择要执行的操作 ##########\n"
    printf "1: 展示将拉取的内容\n"
    printf "2: 拉取传输\n"
    printf "q: 退出\n\n"
    read -p "请输入要执行的选项：" choice
    if [ "$choice" = "1" ]; then
        preview
    elif [ "$choice" = "2" ]; then
        promt
    elif [ "$choice" = "q" ]; then
        abort
    else
        menu
    fi
}

promt(){
    printf "确定要将远程存储的内容拉取<<<==到本地吗？\n"
    printf "y: 拉取\n"
    printf "n: 取消拉取并返回菜单\n"
    printf "q: 直接退出\n\n"
    read -p "请选择：" choice
    if [ "$choice" = "y" ]; then
        run
    elif [ "$choice" = "n" ]; then
        menu
    elif [ "$choice" = "q" ]; then
        abort
    else
        promt
    fi
}

preview(){
    rsync -avtn --delete --mkpath --exclude-from '../#%exclude_file_name%#' $RSYNC_USER@$DOMAIN::$RSYNC_MODULE/$SERVER_DIR/data/ $CLIENT_PATH
    menu
}

run(){
    rsync -avt --delete --mkpath --exclude-from '../#%exclude_file_name%#' $RSYNC_USER@$DOMAIN::$RSYNC_MODULE/$SERVER_DIR/data/ $CLIENT_PATH
    abort
}

abort(){
    exit
}

menu